package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.FDDbNote;

public class FDDbNoteDS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	public FDDbNoteDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	@SuppressWarnings("static-access")
	public int createOrUpdateFDDbNote(ArrayList<FDDbNote>  list) {
		int count =0;		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		for (FDDbNote fdDbNote : list) {
			
		ContentValues values = new ContentValues();
	
		values.put(dbHelper.FDDBNOTE_RECORD_ID, fdDbNote.getFDDBNOTE_RECORD_ID());
		values.put(dbHelper.FDDBNOTE_REFNO, fdDbNote.getFDDBNOTE_REFNO());
		values.put(dbHelper.FDDBNOTE_REF_INV, fdDbNote.getFDDBNOTE_REF_INV());
		values.put(dbHelper.FDDBNOTE_SALE_REF_NO, fdDbNote.getFDDBNOTE_SALE_REF_NO());
		values.put(dbHelper.FDDBNOTE_MANU_REF, fdDbNote.getFDDBNOTE_MANU_REF());
		values.put(dbHelper.FDDBNOTE_TXN_TYPE, fdDbNote.getFDDBNOTE_TXN_TYPE());
		values.put(dbHelper.FDDBNOTE_TXN_DATE, fdDbNote.getFDDBNOTE_TXN_DATE());
		values.put(dbHelper.FDDBNOTE_CUR_CODE, fdDbNote.getFDDBNOTE_CUR_CODE());
		values.put(dbHelper.FDDBNOTE_CUR_RATE, fdDbNote.getFDDBNOTE_CUR_RATE());
		values.put(dbHelper.FDDBNOTE_DEB_CODE, fdDbNote.getFDDBNOTE_DEB_CODE());
		values.put(dbHelper.FDDBNOTE_REP_CODE, fdDbNote.getFDDBNOTE_REP_CODE());
		values.put(dbHelper.FDDBNOTE_TAX_COM_CODE, fdDbNote.getFDDBNOTE_TAX_COM_CODE());
		values.put(dbHelper.FDDBNOTE_TAX_AMT, fdDbNote.getFDDBNOTE_TAX_AMT());
		values.put(dbHelper.FDDBNOTE_B_TAX_AMT, fdDbNote.getFDDBNOTE_B_TAX_AMT());
		values.put(dbHelper.FDDBNOTE_AMT, fdDbNote.getFDDBNOTE_AMT());
		values.put(dbHelper.FDDBNOTE_B_AMT, fdDbNote.getFDDBNOTE_B_AMT());
		values.put(dbHelper.FDDBNOTE_TOT_BAL, fdDbNote.getFDDBNOTE_TOT_BAL());
		values.put(dbHelper.FDDBNOTE_TOT_BAL1, fdDbNote.getFDDBNOTE_TOT_BAL1());
		values.put(dbHelper.FDDBNOTE_OV_PAY_AMT, fdDbNote.getFDDBNOTE_OV_PAY_AMT());
		values.put(dbHelper.FDDBNOTE_REMARKS, fdDbNote.getFDDBNOTE_REMARKS());
		values.put(dbHelper.FDDBNOTE_CR_ACC, fdDbNote.getFDDBNOTE_CR_ACC());
		values.put(dbHelper.FDDBNOTE_PRT_COPY, fdDbNote.getFDDBNOTE_PRT_COPY());
		values.put(dbHelper.FDDBNOTE_GL_POST, fdDbNote.getFDDBNOTE_GL_POST());
		values.put(dbHelper.FDDBNOTE_GL_BATCH, fdDbNote.getFDDBNOTE_GL_BATCH());
		values.put(dbHelper.FDDBNOTE_ADD_USER, fdDbNote.getFDDBNOTE_ADD_USER());
		values.put(dbHelper.FDDBNOTE_ADD_DATE, fdDbNote.getFDDBNOTE_ADD_DATE());
		values.put(dbHelper.FDDBNOTE_ADD_MACH, fdDbNote.getFDDBNOTE_ADD_MACH());
		values.put(dbHelper.FDDBNOTE_REFNO1, fdDbNote.getFDDBNOTE_REFNO1());
		
		count = (int) dB.insert(dbHelper.TABLE_FDDBNOTE, null, values);
	
		}
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return count;
	}
	
	@SuppressWarnings("static-access")
	public int deleteAll(){
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FDDBNOTE, null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_FDDBNOTE, null, null);
				Log.v("Success", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
}
